%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Yifan Zhang
% Bejing University of Technology
% Copy Right 2023
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calulate Circular Cross Correlation
% 
% 计算交叉验证值，用于之后计算两图旋转角度
%
% INPUT: 
%   HS1, HS2: Hough Spectrum
%
% OUTPUT
%   result: 1 * 180 的一个向量
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function result = circular_cross_correlation(HS1, HS2)

% Check if the inputs have the same length
assert(length(HS1) == length(HS2), 'Input vectors must have the same length.');

s = length(HS1);
result = zeros(1, s);

for i = 1:s
    k = i-1;
    for j = 1:s
        result(i) = result(i) + HS1(j) * HS2(mod(k, s)+1);
        k = k-1;
        if k < 0
            k = s-1;
        end
    end
end

% Normalize the result
result = (result - min(result)) / (max(result) - min(result));

% 绘制霍夫频谱图
smooth_result = smooth(result);
% for i = 1:2
%     smooth_result = smooth(smooth_result);
% end

figure;
% 假设result是一个180*1的矩阵
x = 0:179; % 横坐标为0到179
y = smooth_result; % 纵坐标为result中的元素
plot(x,y); % 绘制折线图
xlabel('x'); % 设置横坐标标签
ylabel('y'); % 设置纵坐标标签
axis([0 180 0 1]); % 设置坐标轴范围

end
